﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RpcGenerator.AS3
{
    class AS3InterfaceGenerator
    {
        public static void Generate(Type type, IMessageSerializer serializer, List<MessageDescription> allDescriptions)
        {
            List<MessageDescription> requestDescriptions = allDescriptions.Where(desc => desc.IsRequest == true).ToList();
            serializer.NewLine(string.Format("public interface {0}",type.Name));
            serializer.BeginBlock();
            requestDescriptions.ForEach(description => 
            {
                serializer.NewLine(string.Format("function {0}(",description.Method.Method.Name));                   
                if (description.Fields.Count > 0)
                {
                    MessageFieldDescription lastField = description.Fields[description.Fields.Count - 1];
                    description.Fields.ForEach(fieldDescription =>
                    {
                        if (lastField != fieldDescription)
                        {
                            serializer.Apppend(string.Format("{1}:{0},", fieldDescription.IsGenericType() ? "Array" : RpcGeneratorHelper.GetParameterTypeName(fieldDescription.Type), fieldDescription.Name));
                        }
                        else
                        {
                            serializer.Apppend(string.Format("{1}:{0}", fieldDescription.IsGenericType() ? "Array" : RpcGeneratorHelper.GetParameterTypeName(fieldDescription.Type), fieldDescription.Name));
                        }
                    });
                }

                serializer.Apppend(string.Format("):{0}", RpcGeneratorHelper.GetParameterTypeName(description.Method.Method.ReturnType)));
            });
            serializer.EndBlock();
        }

        public static void GenerateCallback(Type type, IMessageSerializer serializer, List<MessageDescription> allDescriptions)
        {
            List<MessageDescription> responseDescriptions = allDescriptions.Where(desc => desc.IsRequest == false).ToList();
            if (responseDescriptions.Count > 0)
            {
                serializer.NewLine(string.Format("public interface {0}Callback", type.Name));
                serializer.BeginBlock();
                responseDescriptions.ForEach(description =>
                {
                    GenerateMethod(description, serializer);
                });
                serializer.EndBlock();
            }

            Console.WriteLine(serializer.ToString());
        }

        private static void GenerateMethod(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine(string.Format("function On{0}(", description.Method.Method.Name));

            if (description.Fields.Count > 0)
            {
                MessageFieldDescription lastField = description.Fields[description.Fields.Count - 1];
                description.Fields.ForEach(fieldDescription =>
                {
                    if (lastField != fieldDescription)
                    {
                        serializer.Apppend(string.Format("{1}:{0},", fieldDescription.IsGenericType() ? "Array" : RpcGeneratorHelper.GetParameterTypeName(fieldDescription.Type), fieldDescription.Name));
                    }
                    else
                    {
                        serializer.Apppend(string.Format("{1}:{0}", fieldDescription.IsGenericType() ? "Array" : RpcGeneratorHelper.GetParameterTypeName(fieldDescription.Type), fieldDescription.Name));
                    }
                });
            }
            serializer.Apppend("):void;");
        }
    }
}
